package com.gxuwz.web.struts2;

import java.util.List;

import javax.annotation.Resource;

import com.gxuwz.annotation.LogMsg;
import com.gxuwz.entity.SysEquipment;
import com.gxuwz.entity.SysLaboratory;
import com.gxuwz.entity.SysUser;
import com.gxuwz.service.IEquipmentService;
import com.gxuwz.service.ILaboratoryService;
import com.gxuwz.util.DateUtils;
import com.gxuwz.util.Page;
import com.gxuwz.web.AbstractBaseController;
import com.gxuwz.web.IBaseController;

public class EquipmentController extends AbstractBaseController implements
		IBaseController {

	private static final long serialVersionUID = -8893411852030110322L;

	private SysEquipment equipment;
	private List<Integer> ids; // 获取复选框的值
	private String finds;

	private String emType;
	private String enName;

	@Resource(name = "equipmentService")
	private IEquipmentService equipmentService;
	@Resource(name = "laboratoryService")
	private ILaboratoryService laboratoryService;

	private SysUser user = null;

	private List<SysLaboratory> getLaboratorys() {
		// 用户只能增加自己所管理的实验室的设备
		String hql = "select l from SysUser u left join u.user_lab l where u.id = ?";
		if ("ROLE_TCH".equals(getRoleName())
				|| "ROLE_TECH".equals(getRoleName())) {
			return laboratoryService.list(hql, getUserId());
		} else {
			return laboratoryService.getAllFromTable();
		}
	}

	// 设备列表
	@Override
	public String listPrompt() {
		/**
		 * 思路 1、判断登陆的用户所属的实验室 2、根据实验室显示设备数据
		 */
		Page<SysEquipment> page = new Page<SysEquipment>(); // 创建分页对象指定泛型
		page.setOnePageNum(10); // 设置单页记录数
		page.setPageNo(pageNo == null ? 1 : pageNo); // 设置当前页数 默认1
		page.setClazz("Equipment");

		// 查找参数
		StringBuffer condition = new StringBuffer(" where 1=1");

		// 设置分页参数
		StringBuffer pageParam = new StringBuffer("");

		// 如果查找类型不为空
		if (emType != null && !"".equals(emType)) {
			condition.append(" and equipType ='" + emType + "' ");

			pageParam.append("&emType=" + emType);
		}
		// 如果查找名称不为空
		if (enName != null && !"".equals(enName)) {
			condition.append(" and equipName like '%" + enName + "%' ");
			pageParam.append("&enName=" + enName);
		}

		if ("ROLE_ADMIN".equals(getRoleName())) {

		} else if ("ROLE_TCH".equals(getRoleName())
				|| "ROLE_TECH".equals(getRoleName())) {
			condition.append(" and lab_id = " + getLabId());
		}
		equipmentService.findPageByCondition(page, condition.toString());
		// 设置分页类，在page.html使用
		request.put("page", page);

		// 设置查找的分页参数
		request.put("pageParam", pageParam);
		setPrompt("/WEB-INF/pages/equipment/equipment_list.jsp");
		return SUCCESS;
	}

	/**
	 * 获取角色名
	 * 
	 * @return
	 */
	public String getRoleName() {
		user = (SysUser) session.get("user");
		return user.getUser_role().iterator().next().getRoleName();
	}

	/**
	 * 获取实验室ID
	 * 
	 * @return
	 */
	public int getLabId() {
		user = (SysUser) session.get("user");
		return user.getUser_lab().iterator().next().getId();
	}

	/**
	 * 获取用户ID
	 * 
	 * @return
	 */
	public int getUserId() {
		user = (SysUser) session.get("user");
		return user.getId();
	}

	/**
	 * 跳转到增加设备的页面 思路： 1、判断登陆的用户所属的实验室 2、显示该用户所管理全部实验室供选择
	 */
	@Override
	public String addPrompt() {
		session.put("laboratorys", getLaboratorys()); // 将实验室数据传到前台
		setPrompt("/WEB-INF/pages/equipment/equipment_save.jsp");
		return SUCCESS;
	}

	@Override
	@LogMsg(msg = "新增设备")
	public String doSave() {
		SysUser user = (SysUser) session.get("user");
		equipment.setCreateUserName(user.getUserName());
		equipment.setCreateDate(DateUtils.getCurrentDate());
		equipmentService.save(equipment);
		setPrompt("Equipment_listPrompt.action");
		return "reposition";
	}

	@Override
	@LogMsg(msg = "删除设备")
	public String doBatchDelete() {
		equipmentService.batchDelete(ids);
		setPrompt("Equipment_listPrompt.action");
		return "reposition";
	}

	@Override
	public String updatePrompt() {
		Integer id = equipment.getId();
		SysEquipment equip = equipmentService.load(id);
		session.put("updateEquipment", equip);
		session.put("laboratorys", getLaboratorys());
		setPrompt("/WEB-INF/pages/equipment/equipment_update.jsp");

		return SUCCESS;
	}

	@Override
	@LogMsg(msg = "更新设备")
	public String doUpdate() {
		SysUser user = (SysUser) session.get("user");
		equipment.setCreateUserName(user.getUserName());
		equipment.setCreateDate(DateUtils.getCurrentDate());
		equipment.setLaboratory(laboratoryService.load(equipment
				.getLaboratory().getId()));
		equipmentService.update(equipment);
		setPrompt("Equipment_listPrompt.action");
		return "reposition";
	}

	public String AllQR() {
		List<SysEquipment> equipmentList = equipmentService.getAllFromTable();
		request.put("equipmentList", equipmentList);
		setPrompt("/WEB-INF/pages/equipment/equipment_qr_list.jsp");
		return SUCCESS;
	}

	public SysEquipment getEquipment() {
		return equipment;
	}

	public void setEquipment(SysEquipment equipment) {
		this.equipment = equipment;
	}

	public List<Integer> getIds() {
		return ids;
	}

	public void setIds(List<Integer> ids) {
		this.ids = ids;
	}

	public String getFinds() {
		return finds;
	}

	public void setFinds(String finds) {
		this.finds = finds;
	}

	public String getEmType() {
		return emType;
	}

	public void setEmType(String emType) {
		this.emType = emType;
	}

	public String getEnName() {
		return enName;
	}

	public void setEnName(String enName) {
		this.enName = enName;
	}

}
